//
//  WCNOrdersVC.m
//  WCN
//
//  Created by 杨鑫 on 15/7/8.
//  Copyright (c) 2015年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCNOrdersVC.h"
//
#import "YXSwitchControl.h"
#import "WCNOrderCategoryTableVC.h"
//
#import "Macros.h"
#import "Constants.h"
#import "WCNTabBarVC.h"
//#import "WCNNavigationController.h"
#import "WCPopupLayerManager.h"
@interface WCNOrdersVC ()
{
    UITableView *_currentTable;
    UITableView *_preTable;
    UIView *_contentView;
 
}
@property (weak, nonatomic) YXSwitchControl *catetorySwitch;
@property (nonatomic) NSInteger currentCategory;
@property (strong, nonatomic) WCNOrderCategoryTableVC *processingTable;
@property (strong, nonatomic) WCNOrderCategoryTableVC *completedTable;
@property (strong, nonatomic) WCNOrderCategoryTableVC *unComfirmTable;

@end

@implementation WCNOrdersVC

- (instancetype)init
{
    self = [super init];
    if (self) {
       self.currentCategory = 0;
        
        //订单刷新
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshTable:) name:OrderRefreshNotification object:nil];
//        用户退出通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resetTable) name:UserLogoutNotification object:nil];
    }
    return self;
}

- (void)loadView
{
    [super loadView];
    
    YXSwitchControl *segment = [[YXSwitchControl alloc] init];
    self.catetorySwitch = segment;
    self.navigationItem.titleView = segment;
//    [segment setSelectedIndex:self.currentCategory-1];
    weakSelf_define;
    segment.itemChoosedBlock = ^(NSInteger itemIndex){
        weakSelf.currentCategory = itemIndex;
        [weakSelf switchTab];
    };
    
    WCNOrderCategoryTableVC *unComfirmTable =[[WCNOrderCategoryTableVC alloc] initWithCategory:0];
    WCNOrderCategoryTableVC *processingTable = [[WCNOrderCategoryTableVC alloc] initWithCategory:1];
    WCNOrderCategoryTableVC *completedTable = [[WCNOrderCategoryTableVC alloc] initWithCategory:2];
    
    
    self.unComfirmTable = unComfirmTable;
    self.processingTable = processingTable;
    self.completedTable = completedTable;
    
    unComfirmTable.navigationController = self.navigationController;
    processingTable.navigationController = self.navigationController;
    completedTable.navigationController = self.navigationController;
    
    CGRect frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight-64);
    unComfirmTable.tableView.frame =frame;
    processingTable.tableView.frame = frame;
    completedTable.tableView.frame = frame;
  

    _currentTable =unComfirmTable.tableView;
    [self.view addSubview:unComfirmTable.tableView];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kAppBg;
    // Do any additional setup after loading the view.
//       [self.catetorySwitch addTarget:self action:@selector(switchOrderTable:) forControlEvents:UIControlEventValueChanged];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self switchTab];
}

//- (void)switchOrderTable:(YXSwitchControl *)sender
//{
//    self.currentCategory = sender.selectedIndex;
//    
//    [self switchTab];
//}

- (void)refreshTable:(NSNotification *)note
{
    NSDictionary *info = note.userInfo;
    int index = [info[@"tab"] intValue];
    
//    [self.catetorySwitch setSelectedIndex:index];
    self.currentCategory = index;
    [self switchTab];
    
    [self.unComfirmTable refreshTable];
    [self.processingTable refreshTable];
    [self.completedTable refreshTable];
}

- (void)resetTable
{
    [self.unComfirmTable resetData];
    [self.processingTable resetData];
    [self.completedTable resetData];
}

#pragma mark - ChangeTab

//切换表格式图
- (void)switchTab
{
    UITableView *fromView =_currentTable, *toView;
    WCNOrderCategoryTableVC *willShowVC ;
    
    if (self.currentCategory == 0) {
        toView =self.unComfirmTable.tableView;
        willShowVC = self.unComfirmTable;
    }else if(self.currentCategory == 1){
         toView =self.processingTable.tableView;
         willShowVC = self.processingTable;
       
    }else if(self.currentCategory == 2){
        toView =self.completedTable.tableView;
        willShowVC = self.completedTable;
        
    }
    if (fromView == toView) {
        [willShowVC viewWillShow];
        return;
    }
    
    UIWindow *window =[[UIApplication sharedApplication].delegate window];
     MBProgressHUD *hud= [[WCPopupLayerManager shareMg] showSimple:window];
    [UIView transitionFromView:fromView
                        toView:toView
                      duration:0.5f
                       options:UIViewAnimationOptionTransitionCurlUp
                    completion:^(BOOL finished){
                        [willShowVC viewDidShow];
                        _currentTable =(UITableView *)toView;
                        
                        [hud hide:YES];
                    }
     ];
    [willShowVC viewWillShow];
}

@end
